package dk.eclipse.android.badassapp;

import java.util.Locale;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import dk.eclipse.android.badassapp.mission.MissionActivity;
import dk.eclipse.android.badassapp.preferences.PreferenceActivity;
import dk.eclipse.android.badassapp.preferences.PreferenceHelper;
import dk.eclipse.android.badassapp.sensor.SensorActivity;
import dk.eclipse.android.badassapp.target.TargetActivity;
import dk.eclipse.android.badassapp.weapon.WeaponActivity;

/**
 * @author Ronni
 * 
 */
public class BadAssAppActivity extends TabActivity {
	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = BadAssAppActivity.class
			.getSimpleName();

	/**
	 * Enabled text to speech.
	 */
	private static TextToSpeech text2Speech;
	private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() {

		public void onInit(int status) {
			// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
			if (status == TextToSpeech.SUCCESS) {
				int result = text2Speech.setLanguage(Locale.US);

				if (result == TextToSpeech.LANG_MISSING_DATA
						|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
					// Language data is missing or the language is not
					// supported.
					Log.e(LOG_TAG, "Language is not available.");
				} else {
					// Speak to the user.
					speak("Hello, "
							+ PreferenceHelper
									.getFighterName(BadAssAppActivity.this
											));
				}
			} else {
				// Initialization failed.
				Log.e(LOG_TAG, "Could not initialize TextToSpeech.");
			}
		}
	};

	private TabDef[] tabDefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.tabhost);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.ic_app);

		// Initialize speech.
		text2Speech = new TextToSpeech(this, ttsInitListener);

		final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

		tabDefs = new TabDef[] {
				new TabDef(getString(R.string.missionTabId),
						R.drawable.tab_mission_on,
						getString(R.string.missionTabTitle),
						MissionActivity.class),
				new TabDef(getString(R.string.weaponTabId),
						R.drawable.tab_weapon_on,
						getString(R.string.weaponTabTitle),
						WeaponActivity.class),
				new TabDef(getString(R.string.targetTabId),
						R.drawable.tab_target_on,
						getString(R.string.targetTabTitle),
						TargetActivity.class)};

		Resources resources = getResources();
		for (TabDef tabDef : tabDefs) {
			TabSpec tabSpec = tabHost.newTabSpec(tabDef.specId);
			tabSpec.setIndicator(tabDef.getTitle(),
					resources.getDrawable(tabDef.getIconId())).setContent(
					new Intent(this, tabDef.getClazz()));
			tabHost.addTab(tabSpec);

		}

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String arg0) {

				// Speak the tab title.
				String title = tabDefs[tabHost.getCurrentTab()].title;

				Log.i("******SEAKING TAB TITLR", "------" + title);

				speak(title);

			}
		});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;

		// Handle item selection
		switch (item.getItemId()) {

		case R.id.selfdestructMenuItem:
			return true;

		case R.id.sensorsMenuItem:
			intent = new Intent(this, SensorActivity.class);
			//intent = new Intent(this, SensorVisualizerActivity.class);
			//intent = new Intent(this, CompassActivity.class);
			startActivity(intent);
			
			return true;

		case R.id.settingsMenuItem: 
			intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
			startActivity(intent);

			return true;

		case R.id.preferencesMenuItem: 
			intent = new Intent(this,
					PreferenceActivity.class);
			startActivity(intent);
			
			return true;

			
		case R.id.quitMenuItem:
			finish();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Speaks the text message to voice.
	 * 
	 * @param message
	 */
	private void speak(final String message) {
		
		new Thread("Speak: " + message) {

			/* (non-Javadoc)
			 * @see java.lang.Thread#run()
			 */
			@Override
			public void run() {

				// Drop all pending entries in the playback queue.
				text2Speech.speak(message, TextToSpeech.QUEUE_FLUSH, null);

				// This way you can write the speech to file.
				// text2Speech.synthesizeToFile(message,null,
				// "/sdcard/myappsounds/mysms.wav");

			}
			
		}.start();
	}

	/**
	 * Listens for text to speech events.
	 */

	@Override
	public void onDestroy() {

		if (text2Speech != null) {
			text2Speech.stop();
			text2Speech.shutdown();
			text2Speech = null;
		}
		super.onDestroy();
	}

	private class TabDef {
		private String specId;
		private String title;
		private Class<?> clazz;
		private int iconId;

		public TabDef(String specId, int iconId, String title, Class<?> clazz) {
			super();
			this.specId = specId;
			this.iconId = iconId;
			this.title = title;
			this.clazz = clazz;
		}

		/**
		 * @return the iconId
		 */
		public int getIconId() {
			return iconId;
		}

		/**
		 * @return the specId
		 */
		public String getSpecId() {
			return specId;
		}

		/**
		 * @return the title
		 */
		public String getTitle() {
			return title;
		}

		/**
		 * @return the clazz
		 */
		public Class getClazz() {
			return clazz;
		}

	}
}